package com.junxonline.instant.admin.controller.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.junxonline.instant.dao.entity.model.config.Region;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.RegionVO;
import com.junxonline.instant.admin.service.config.RegionService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 区域管理Controller
 *
 * @author JunX
 * @date 2021-07-18
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    /**
     * 查询上级菜单下拉列表（Select）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:region:query")
    @OperationLog(label = "区域管理-查询上级菜单下拉列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryParentList")
    public CommonResult queryParentList(@RequestBody(required = false) RegionVO vo) {
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(vo.getType() != null, Region::getType, vo.getType());
        return CommonResult.success(regionService.querySelect(wrapper));
    }

    /**
     * 查询区域列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:region:query")
    @OperationLog(label = "区域管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<RegionVO> vo) {
        return CommonResult.success(regionService.queryList(vo));
    }

    /**
     * 查询区域
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:region:query")
    @OperationLog(label = "区域管理-查询区域", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) RegionVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(regionService.query(vo.getId()));
    }

    /**
     * 新增区域
     *
     * @param region 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:region:add")
    @OperationLog(label = "区域管理-新增区域", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) Region region) {
        if (region == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        regionService.add(region);
        return CommonResult.success();
    }

    /**
     * 编辑区域
     *
     * @param region 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:region:edit")
    @OperationLog(label = "区域管理-编辑区域", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Region region) {
        if (region == null || region.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        regionService.edit(region);
        return CommonResult.success();
    }

    /**
     * 删除区域
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:region:delete")
    @OperationLog(label = "区域管理-删除区域", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        regionService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
